#include<iostream>
using namespace std;
#include<vector>
class NeighborSum {
public:
    int x1[4] = { -1,1,0,0 }, y1[4] = { 0,0,-1,1 };
    int x2[4] = { -1,-1,1,1 }, y2[4] = { -1,1,-1,1 };
    vector<vector<int>> matrix;
    int n;
    NeighborSum(vector<vector<int>>& grid) {
        matrix = grid;
        n = grid.size();
    }

    int adjacentSum(int value) {
        int sum = 0;
        int row, line;
        for (int i = 0; i < n; ++i) {
            int flag = 0;
            for (int j = 0; j < n; ++j) {
                if (matrix[i][j] == value) {
                    row = i;
                    line = j;
                    flag = 1;
                    break;
                }
            }
            if (flag)
                break;
        }
        for (int i = 0; i < 4; ++i) {
            int x = x1[i] + row, y = y1[i] + line;
            if (x >= 0 && x < n && y >= 0 && y < n)
                sum += matrix[x][y];
        }
        return sum;
    }

    int diagonalSum(int value) {
        int sum = 0;
        int row, line;
        for (int i = 0; i < n; ++i) {
            int flag = 0;
            for (int j = 0; j < n; ++j) {
                if (matrix[i][j] == value) {
                    row = i;
                    line = j;
                    flag = 1;
                    break;
                }
            }
            if (flag)
                break;
        }
        for (int i = 0; i < 4; ++i) {
            int x = x2[i] + row, y = y2[i] + line;
            if (x >= 0 && x < n && y >= 0 && y < n)
                sum += matrix[x][y];
        }
        return sum;
    }
};

/**
 * Your NeighborSum object will be instantiated and called as such:
 * NeighborSum* obj = new NeighborSum(grid);
 * int param_1 = obj->adjacentSum(value);
 * int param_2 = obj->diagonalSum(value);
 */
